package com.example.lp.lpuicore.views.touch.screencheck;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lp.lpuicore.R;

import java.util.List;

/**
 * Author by lp,on 2021/2/24/024,11:49.
 */
public class CheckAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "CheckAdapter";
    private List<Boolean> checkList;
    private ChcekListtner chcekListtner;

    private Drawable tochDrable;
    private int height = 0;


    public interface ChcekListtner {
        void checkResult(Boolean allCheck);
    }

    public CheckAdapter(Context context, int Rvheight, int colum, List<Boolean> checkList, ChcekListtner chcekListtner) {
        tochDrable = context.getDrawable(R.drawable.check_touch);
        this.height = Rvheight / colum;
        Log.i(TAG, "CheckAdapter: Rvheight" + Rvheight);
        Log.i(TAG, "CheckAdapter: height" + this.height);
        this.checkList = checkList;
        this.chcekListtner = chcekListtner;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_screen_check, viewGroup,
                false));
//        holder.checkBox.setOnClickListener(this);
        holder.itemView.getLayoutParams().height = height;
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        //设置标记为当前位置id
        holder.root.setTag(position);
        holder.check.setText(String.valueOf(position));
    }

    @Override
    public int getItemCount() {
        return checkList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView check;
        private RelativeLayout root;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            check = itemView.findViewById(R.id.check);
            root = itemView.findViewById(R.id.root);
        }
    }

    //检测是否全部选中
    public Boolean moveCheck(View view) {
        RelativeLayout root = (RelativeLayout) view;
        Log.i(TAG, "moveCheck: check");
        root.setBackground(tochDrable);
        checkList.set((Integer) root.getTag(), true);
        Boolean result = true;
        for (Boolean check : checkList) {
            if (!check) {
                result = false;
            }
        }
        if (chcekListtner != null) {
            chcekListtner.checkResult(result);
        }
        return result;
    }
}
